package ps.client.plugin.eq2.gui.dialog.curses;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JPanel;

import ps.client.plugin.eq2.gui.dialog.curses.timer.Timer;
import ps.client.plugin.eq2.gui.dialog.curses.timer.TimerEvent;
import ps.client.plugin.eq2.gui.dialog.curses.timer.TimerListener;



@SuppressWarnings("serial")
public class CountdownPanel extends JPanel implements TimerListener {
    
    int timerSecs;
    
    JLabel timeLabel = new JLabel();
    
    Timer timer = new Timer();
    
    public CountdownPanel() {
    	this(40);
    }
    
    public CountdownPanel(int timerSecs) {
    	this.timerSecs = timerSecs;
    	
    	setLayout(new BorderLayout());
    	add(timeLabel, BorderLayout.CENTER);
    	
        timeLabel.setFont(new Font("Courier New", Font.BOLD, 14));
//        timeLabel.setHorizontalAlignment(JLabel.RIGHT);
        
        timer.addTimerListener(this);
        
        Thread timerThread = new Thread(timer, "TimerThread");
        timerThread.start();
    }
    
    public void timerEvent(TimerEvent e) {
    	setNewTime(e.getSecs());
    }
    
	public Timer getTimer() {
		return timer;
	}
    
    public void setTimer(int timerSecs) {
    	this.timerSecs = timerSecs;
    	setNewTime(0);
    }
    
    public JLabel getTimerLabel() {
    	return timeLabel;
    }
    
    void setNewTime(int secs) {
    	int leftSecs = timerSecs - secs;
    	if (leftSecs <= 0) {
    		leftSecs = 0;
    		timer.stop();
    	}
    	timeLabel.setText(TimerPanel.generateTimeString(leftSecs));
    }
    
    public void setLeftSecsToZero() {
        setNewTime(timerSecs);
    }
    
    public void dispose() {
    	timer.dispose();
    }
}
